const express = require('express');
const router = express.Router();
const menusDao = require("../dao/mysql/MenusDao");
const pager = require("../util/PagerHelper.js");

router.get("/getAll.do", async (req, res, next) => {
    menusDao.findBy().then(data => {
        res.json({
            data,
            code: 200
        });
    }).catch(err => next(err));
});

router.all("/list.do", async (req, res, next) => {
    let where = "";
    let flag = 0;//控制多条件是否要加and
    if (req.body) {
        console.log("req.body", req.body);
        if (req.body.username) {
            if (where.trim().length === 0) {
                where += "where ";
            }
            where += `username like '%${req.body.username}%' `;
            flag++;
        }
        if (req.body.phone) {
            if (where.trim().length === 0) {
                where += "where ";
            }
            if (flag > 0) {
                where += " and "
            }
            where += ` phone like '%${req.body.phone}%' `;
            flag++;
        }
        if (req.body.gender) {
            if (where.trim().length === 0) {
                where += "where ";
            }
            if (flag > 0) {
                where += " and "
            }
            where += ` gender = '${req.body.gender}' `;
            flag++;
        }
        if (req.body.dormId) {
            if (where.trim().length === 0) {
                where += "where ";
            }
            if (flag > 0) {
                where += " and "
            }
            where += ` dormId = '${req.body.dormId}' `;
            flag++;
        }
        console.log(where);
    }
    menusDao.findBy().then(data => {
        res.locals.dorms = data;
    }).catch(err => next(err));
    dao.getCount(where).then(count => {
        console.log("count", count);
        pager.setCount(count);
        return dao.findByPage(where);
    }).then(data => {
        console.log("data", data);
        pager.data = data;
        res.json({
            code: 200,
            data:pager
        });
    })
        .catch(err => next(err));
});

router.get("/del.do", (req, res, next) => {
    // lodash.remove(books, value => value.id == req.query.id);
    if (req.query.id) {
        menusDao.deleteById(req.query.id).then(data => res.redirect("/book/list.html")).catch(err => next(err));
    } else {
        res.redirect("/book/list.html");
    }
});
router.get("/delBatch.do", (req, res, next) => {
    let idsStr = req.query.ids;
    console.log(idsStr);
    let ids = idsStr.split(",");//得到的是一个字符串数组
    // lodash.remove(books, value => ids.some(id => id == value.id));
    dao.deleteByIds(ids).then(data => res.redirect("/book/list.html")).catch(err => next(err));
});
router.post("/save.do", (req, res, next) => {
    let newObj = Object.assign({}, req.body);
    //区分添加还是编辑
    if (newObj.id) {
        //编辑
        // let editObj = books.find(value => value.id == newObj.id);
        // //引用数据类型的特点
        // Object.assign(editObj, newObj);
        menusDao.update(newObj, newObj.id).then(data => res.redirect("/book/list.html")).catch(err => next(err));
    } else {
        //添加
        // // books.pop();删除最后一个元素
        // newObj.id = lodash.last(books).id + 1;
        // //保存到数组中
        // books.push(newObj);
        newObj.id = 0;//id是自动增长，必须是0值
        dao.insert(newObj).then(data => res.redirect("/book/list.html")).catch(err => next(err));
    }
});

module.exports = router;
